package com.ksd.modules.rbac.sysuser.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.modules.rbac.sysuser.entity.SysUsers;
import com.ksd.modules.rbac.sysuser.po.SysUsersSavePO;
import com.ksd.modules.rbac.sysuser.po.SysUsersSearchPO;
import com.ksd.modules.rbac.sysuser.service.ISysUsersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@RestController
public class SysUsersController extends BaseController {

    @Autowired
    private ISysUsersService sysUsersService;

    /**
     * 查询首页有系统日志接口
     *
     * @param adminSysUsersSearchPO
     * @return
     */
    @PostMapping("/sysusers/list")
    public R<Page<SysUsers>> findSysUsers(@Validated @RequestBody SysUsersSearchPO adminSysUsersSearchPO) {
        return R.data(sysUsersService.findSysUsers(adminSysUsersSearchPO));
    }

    /**
     * 解决绑定老师关系
     * @return
     */
    @PostMapping("/sysusers/unbind/{id}")
    public R<Boolean> unbindTeacherFlag(@PathVariable("id") Long id) {
        return R.data(sysUsersService.unbindTeacherFlag(id));
    }

    /**
     * 保存和更新
     *
     * @param adminSysUsersSavePO
     * @return
     */
    @PostMapping("/sysusers/saveupdate")
    @ApiLog("系统用户保存和更新")
    public R<Boolean> saveUpdateSysUsers(@Validated @RequestBody SysUsersSavePO adminSysUsersSavePO) {
        return R.data(sysUsersService.saveUpdateSysUsers(adminSysUsersSavePO));
    }

    /**
     * 根据id查询系统用户
     *
     * @param id
     * @return
     */
    @PostMapping("/sysusers/get/{id}")
    public R<SysUsers> getSysUsersById(@PathVariable("id") Long id) {
        return R.data(sysUsersService.getSysUsersById(id));
    }

    /**
     * 根据id删除系统用户
     *
     * @param id
     * @return
     */
    @PostMapping("/sysusers/del/{id}")
    @ApiLog("根据id删除系统用户")
    public R<Boolean> delSysUsersById(@PathVariable("id") Long id) {
        return R.data(sysUsersService.deleteSysUsersById(id));
    }


    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/sysusers/status/{id}/{field}/{value}")
    @ApiLog("系统用户修改状态")
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(sysUsersService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(sysUsersService
                    .updateStatusStr(id, field, String.valueOf(value)));
        }
    }


    /**
     * 修改默认密码
     * @return
     */
    @PostMapping("/sysusers/updatepwd/{id}")
    @ApiLog("修改默认密码")
    public R<Boolean> updateDefaultPwd(@PathVariable("id") Long id) {
        return R.data(sysUsersService.updatePasswordDefault(id));
    }
}
